package com.pcbly.web.action.user;

import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.pcbly.web.action.BaseAction; 
import com.pcbly.web.bean.system.DepartBean;
import com.pcbly.web.bean.system.GroupBean;
import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.bean.user.UserDetailBean;
import com.pcbly.web.bean.user.UserRelationBean;
import com.pcbly.web.bean.user.UserSchoolBean;
import com.pcbly.web.bean.user.UserWorkExperienceBean;
import com.pcbly.web.business.depart.DepartService;
import com.pcbly.web.business.group.GroupService;
import com.pcbly.web.business.user.UserService;
import com.pcbly.web.components.utils.security.MD5;
import com.pcbly.web.components.utils.LightsoftStringUtils;

public class UserAction extends BaseAction<UserBean> implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3687134731024378487L;
	
	private List<UserBean> users;
	
	private UserDetailBean userDetailBean;
	
	private List<UserSchoolBean> userSchoolBeans;
	
	private List<UserRelationBean> userRelationBeans;
	
	private List<UserWorkExperienceBean> userWorkExperienceBeans;
	
	private List<DepartBean> departBeans;
	
	private UserBean user;
	
	private String id;
	
	private String jsonString;
	
	private UserService userService;
	
	private DepartService departService; 
	
	private GroupService groupService;
	
	private List<GroupBean> groupBeans;
	
	
	public List<GroupBean> getGroupBeans() {
		return groupBeans;
	}

	public void setGroupBeans(List<GroupBean> groupBeans) {
		this.groupBeans = groupBeans;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public List<DepartBean> getDepartBeans() {
		return departBeans;
	}

	public void setDepartBeans(List<DepartBean> departBeans) {
		this.departBeans = departBeans;
	}

	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}

	public UserDetailBean getUserDetailBean() {
		return userDetailBean;
	}

	public void setUserDetailBean(UserDetailBean userDetailBean) {
		this.userDetailBean = userDetailBean;
	}

	public List<UserSchoolBean> getUserSchoolBeans() {
		return userSchoolBeans;
	}

	public void setUserSchoolBeans(List<UserSchoolBean> userSchoolBeans) {
		this.userSchoolBeans = userSchoolBeans;
	}

	public List<UserRelationBean> getUserRelationBeans() {
		return userRelationBeans;
	}

	public void setUserRelationBeans(List<UserRelationBean> userRelationBeans) {
		this.userRelationBeans = userRelationBeans;
	}

	public List<UserWorkExperienceBean> getUserWorkExperienceBeans() {
		return userWorkExperienceBeans;
	}

	public void setUserWorkExperienceBeans(
			List<UserWorkExperienceBean> userWorkExperienceBeans) {
		this.userWorkExperienceBeans = userWorkExperienceBeans;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public List<UserBean> getUsers() {
		return users;
	}

	public String edit() throws Exception {
		try{
		if (log.isDebugEnabled()) {
			log.debug("login edit");
		}
		departBeans=departService.findDepartBeansByParentId("1");
		DepartBean option=new DepartBean();
		option.setId("");
		option.setDepartName("-------------------");
		departBeans.add(0, option);
		user=userService.getUserBeanById(getId());
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return super.edit();
	}

	public String save() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login save "+user.getId());
		} 		
		if(!(LightsoftStringUtils.nullOrBlank(user.getId()))){
			if(LightsoftStringUtils.nullOrBlank(user.getUserDetail().getId())){
				user.getUserDetail().setId(user.getId());
			} 
			user=userService.updateUserBean(user);
			saveMessage(getText("entity.updated"));
		}else{
			boolean iflag=LightsoftStringUtils.nullOrBlank(user.getPasswd());
			iflag=iflag||LightsoftStringUtils.nullOrBlank(user.getRePasswd());
			if(!iflag&&user.getPasswd().trim().equals(user.getRePasswd().trim())){
				user.setPasswd(MD5.getMD5(user.getPasswd().trim().getBytes()));
				user.setRePasswd(user.getPasswd()); 
				user=userService.saveUserBean(user);
				saveMessage(getText("entity.saved"));
			}
//			else{
//				addFieldError("user.passwd", getText("user.passwdAndRePasswd",getText("user.passwd"),getText("user.rePasswd"))); 
//			}
		}
		return super.save();
	}
	
	public String add() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login add");
		}
		departBeans=departService.findDepartBeansByParentId("1");
		DepartBean option=new DepartBean();
		option.setId("");
		option.setDepartName("-------------------");
		departBeans.add(0, option);
		return super.add();
	}
	
	public String index() throws Exception {
//		System.out.println("login index");
		if (log.isDebugEnabled()) {
			log.debug("login index");
		}
		users = userService.findUserBeanAll();
		// setJsonString(JsonUtils.object2json(users)); //not write
		return super.index();
	}

	public String getId() {
		return id;
	}

	public void setUsers(List<UserBean> users) {
		this.users = users;
	}

	public void setId(String id) {
		this.id = id;
	}

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if (getRequest().getMethod().equalsIgnoreCase("post")) {
			if (!"".equals(getRequest().getParameter("user.id"))) {
				// prevent failures on new
				// user =
				// userManager.getUser(getRequest().getParameter("user.id"));
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
